from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect

from django.views.decorators.csrf import csrf_protect
from django.template import Context, loader, RequestContext
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from filemanager.forms import UploadFileForm
from filemanager.models import FileModel
from filemanager.fileupload import handle_uploaded_file
from settings import PATH
import datetime
import os

def index(request): # post index. Display.
    file_list=os.listdir(PATH+'/files')
    template = loader.get_template('filemanager/index.html')
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('success/')
    else:
        form = UploadFileForm()
    data = [('file_list',file_list),('form', form),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))

 
def success(request):
    template = loader.get_template('filemanager/success.html')
    data=[] #empty data for use. 
    context = Context(dict(data))
    return HttpResponse(template.render(context))
